package kr.couponplay.android.support;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

public class StreamUtils {
    public static void copyStream(InputStream is, OutputStream os) {
        final int buffer_size = 1024;
        try {
            byte[] bytes = new byte[buffer_size];
            for(;;) {
              int count=is.read(bytes, 0, buffer_size);
              if(count == -1) {
                  break;
              }
              os.write(bytes, 0, count);
            }
        } catch(Exception ex) {
        	
        }
    }	
	
	public static String streamToString(InputStream is, String encoding) throws IOException {
		BufferedReader br = null;
		StringBuffer buffer = new StringBuffer();
		boolean isSubstring = false;

		try {
			br = new BufferedReader(new InputStreamReader(is, encoding));
			String line; 
			while ((line = br.readLine()) != null) {
				buffer.append(line).append("\n");
				isSubstring = true;
			}
		} finally {
			if (br != null) {
				br.close();
			}
		}
		return (isSubstring) ? buffer.substring(0, buffer.length() - 1) : buffer.toString();
	}
	
	public static File bitmapToJpeg(Bitmap bitmap) {
		File file = new File("uploadImage.jpg");
		try {
		FileOutputStream filestream = new FileOutputStream(file);
		bitmap.compress(CompressFormat.PNG, 0, filestream);
		} catch (FileNotFoundException e) {

		}
		return file;
	}
}
